package com.sweet.fourtimeoa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sweet.fourtimeoa.mapper.UserMapper;
import com.sweet.fourtimeoa.pojo.dto.DocumentList;
import com.sweet.fourtimeoa.pojo.dto.PageDto;
import com.sweet.fourtimeoa.pojo.dto.SystemUserList;
import com.sweet.fourtimeoa.pojo.dto.SystemUserQueryDto;
import com.sweet.fourtimeoa.pojo.dto.PageDto;
import com.sweet.fourtimeoa.pojo.entity.Departments;
import com.sweet.fourtimeoa.pojo.entity.Roles;
import com.sweet.fourtimeoa.pojo.entity.Users;
import com.sweet.fourtimeoa.pojo.vo.PageVo;
import com.sweet.fourtimeoa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public Users login(String username) {
        return userMapper.login(username);
    }

    @Override
    public String getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    @Override
    public PageVo<SystemUserList> querySystemUserList(PageDto<SystemUserQueryDto> pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        Page<SystemUserList> list =(Page<SystemUserList>)userMapper.querySystemUserList(pageDto.getData());
        PageVo<SystemUserList> pageVo = new PageVo<>();
        pageVo.setPageNum(list.getPageNum());
        pageVo.setPageSize(list.getPageSize());
        pageVo.setTotal(list.getTotal());
        pageVo.setPages(list.getPages());
        pageVo.setList(list.getResult());
        return pageVo;
    }

    @Override
    public void insertUser(Users users) {
        userMapper.insertUser(users);
    }

    @Override
    public List<Roles> queryRolesList() {
        return userMapper.queryRolesList();
    }

    @Override
    public List<Departments> queryDepartmentsList() {
        return userMapper.queryDepartmentsList();
    }

    @Override
    public Boolean deleteUser(Integer userId) {
      return   userMapper.deleteUser(userId);
    }

    @Override
    public Users queryUserById(Integer userId) {
        return userMapper.queryUserById(userId);
    }

    @Override
    public void updateUser(Users users) {
        userMapper.updateUser(users);
    }

    @Override
    public String getUserByUserId(Integer userId) {
        return userMapper.getUserByUserId(userId);
    }

    @Override
    public void updatePassword(String password,Integer userId) {
        userMapper.updatePassword(password,userId);
    }


    @Override
    public PageVo<Users> getAllUser(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
      Page<Users> users= (Page<Users>) userMapper.getAllUser();
      PageVo<Users> pageVo = new PageVo<>();
      pageVo.setList(users.getResult());
      pageVo.setTotal(users.getTotal());
      pageVo.setPages(users.getPages());
      pageVo.setPageNum(users.getPageNum());
      pageVo.setPageSize(users.getPageSize());
      return pageVo;
    }
}
